home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctask.zip / TSIO.C < prev    next >
C/C++ Source or Header  |  1988-07-01  |  3KB  |  165 lines

  1.  
  2. /*
  3.    Test program for checking the CTask serial I/O interface.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <ctype.h>
  9. #include <process.h>
  10.  
  11. #include "tsk.h"
  12. #include "sio.h"
  13.  
  14. #if (TSK_NAMED)
  15. extern void snapshot (FILE *f);
  16. #endif
  17.  
  18. #define PORT   0x80     /* COM1, relative */
  19. #define BAUD   9600L    /* Baudrate */
  20.  
  21.  
  22. #define STACKSIZE 2048
  23.  
  24. word _stklen = 4 * STACKSIZE;  /* For Turbo C: Two tasks + main Task Stack */
  25.  
  26. tcb tcb1, tcb2, tcb3;
  27. flag halt;
  28. sioptr siop;
  29.  
  30. word rcvbuf [10000];
  31. byte xmtbuf [100];
  32.  
  33. int endrun, err;
  34.  
  35. /*
  36.    Task 1 reads characters from the serial line and displays them on
  37.    the screen. While the halt flag is set, characters are not read,
  38.    so the XON/XOFF and RTS/CTS protocol can be tested for the receiving 
  39.    side.
  40. */
  41.  
  42. void far task1 (void)
  43. {
  44.    word ch;
  45.  
  46.    printf ("Task 1 started\n");
  47.    while (!endrun)
  48.       {
  49.       wait_flag_clear (&halt, 0L);
  50.       if (endrun)
  51.          return;
  52.       ch = v24_receive (siop, 0L);
  53.       putch (ch);
  54.       if (ch & 0xff00)
  55.          {
  56.          err = 1;
  57.          printf ("\n%c*%02x*", ch, ch >> 8);
  58.          }
  59.       }
  60. }
  61.  
  62.  
  63. /*
  64.    Task 2 reads characters from the keyboard and sends them to the
  65.    serial port. If 'h' is entered, the halt flag is set, so task1
  66.    stops reading. If 'c' is entered, the halt flag is cleared.
  67.    Entering 'e' stops the program.
  68.    'd' outputs snapshot dump.
  69. */
  70.  
  71. void far task2 (void)
  72. {
  73.    int ch;
  74.  
  75.    printf ("Task 2 started\n");
  76.    while (!endrun)
  77.       {
  78.       ch = t_read_key () & 0xff;
  79.       switch (tolower (ch))
  80.          {
  81.          case 'h':   set_flag (&halt);
  82.                      puts ("-halt-");
  83.                      break;
  84.  
  85.          case 'c':   clear_flag (&halt);
  86.                      err = 0;
  87.                      puts ("-continue-");
  88.                      break;
  89.  
  90.          case 'e':   puts ("-end-");
  91.                      endrun = 1;
  92.                      clear_flag (&halt);
  93.                      wake_task (NULL);
  94.                      break;
  95.  
  96. #if (TSK_NAMED)
  97.          case 'd':   snapshot (stdout);
  98.                      break;
  99. #endif
  100.  
  101.          default:    /* putch (ch); */
  102.                      v24_send (siop, ch, 0L);
  103.                      break;
  104.          }
  105.       }
  106. }
  107.  
  108.  
  109. main ()
  110. {
  111.    char stack1 [STACKSIZE];
  112.    char stack2 [STACKSIZE];
  113.  
  114.    endrun = 0;
  115.  
  116.    install_tasker (0, 0);
  117.    siop = v24_install (PORT, 1, rcvbuf, sizeof (rcvbuf), xmtbuf, sizeof (xmtbuf));
  118.  
  119.    if (siop == NULL)
  120.       {
  121.       remove_tasker ();
  122.       printf ("Couldn't install COM-Port\n");
  123.       exit (1);
  124.       }
  125.    v24_change_baud (siop, BAUD);
  126.    v24_protocol (siop, XONXOFF | RTSCTS, 40, 60);
  127.  
  128.    create_task (&tcb1, task1, stack1, STACKSIZE, PRI_STD, NULL
  129. #if (TSK_NAMEPAR)
  130.                 ,"TASK1"
  131. #endif
  132.                 );
  133.    create_task (&tcb2, task2, stack2, STACKSIZE, PRI_STD, NULL
  134. #if (TSK_NAMEPAR)
  135.                 ,"TASK2"
  136. #endif
  137.                 );
  138.  
  139.    create_flag (&halt
  140. #if (TSK_NAMEPAR)
  141.                 ,"Halt"
  142. #endif
  143.                 );
  144.  
  145.    start_task (&tcb1);
  146.    start_task (&tcb2);
  147.  
  148.    preempt_on ();
  149.    t_delay (0L);
  150.  
  151.    endrun = 1;
  152.    puts ("******** Main Task *********");
  153.  
  154.    set_priority (NULL, 10);
  155.    schedule ();
  156.    delete_flag (&halt);
  157.  
  158.    preempt_off ();
  159. /*   v24_remove (siop, 1); */
  160.    remove_tasker ();
  161.  
  162.    puts ("******** End Run *********");
  163. }
  164.  
  165.